package edu.ccut.saturn.studio.database.popup.action;

import java.util.ArrayList;
import java.util.List;


import edu.ccut.saturn.studio.core.util.action.SaturnAction;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnDialogUtil;
import edu.ccut.saturn.studio.database.model.DBConnection;
import edu.ccut.saturn.studio.database.model.DBConnectionProvider;
import edu.ccut.saturn.studio.database.model.SaturnTree;
import edu.ccut.saturn.studio.database.ui.DBConnectView;

/**
 * 删除连接
 * 
 * @author 谷钰
 * @version 1.0 2008.12.06
 * 
 */
public class DelDBConnectionAction extends SaturnAction {

	public void run() {
		List<SaturnTree> selecteds = DBConnectView.getInstance()
				.getCurrentSelections();

		List<DBConnection> deleteConnectoins = new ArrayList<DBConnection>();

		if (selecteds != null) {
			for (SaturnTree selected : selecteds) {
				if (selected instanceof DBConnection) {
					deleteConnectoins.add((DBConnection) selected);
				}
			}
		}

		if (SaturnDialogUtil.openConfirm(super.getShell(), Messages
				.getString("DelDBConnectionAction.deletelink"), //$NON-NLS-1$
				Messages.getString("DelDBConnectionAction.confirmdeletelink"))) { //$NON-NLS-1$

			for (DBConnection connection : deleteConnectoins) {
				try {
					DBConnectionProvider.getInstance().removeDBConnection(
							connection);
					DBConnectView.getInstance().removeDbConnection(connection);
				} catch (Exception e) {
					SaturnDialogUtil.openError(super.getShell(),
							"Delete Connection Error!:" + e.getMessage()); //$NON-NLS-1$
				}
			}
		}
	}
}
